<?php

namespace backend\controllers;

use Yii;
use common\models\Release\Release;
use common\models\Regstand\Regstand;
use common\models\Rezstand\Rezstand;
use common\models\Reguomin\Reguomin;
use common\models\Release\ReleaseSearch;
use yii\web\Controller;
use yii\web\NotFoundHttpException;
use yii\filters\VerbFilter;
use zhongkefu\zhongkefu;
use zhongkefu\helpers\Utils;
use common\models\adminlog\AdminLog;
/**
 * ReleaseController implements the CRUD actions for Release model.
 */
class ReleaseController extends Controller
{
    /**
     * @inheritdoc
     */
    public function behaviors()
    {
        return [
            'verbs' => [
                'class' => VerbFilter::className(),
                'actions' => [
                    'delete' => ['POST'],
                ],
            ],
        ];
    }

    /**
     * Lists all Release models.
     * @return mixed
     */
    public function actionIndex()
    {
        $searchModel = new ReleaseSearch();
        $dataProvider = $searchModel->search(Yii::$app->request->queryParams);

        return $this->render('index', [
            'searchModel' => $searchModel,
            'dataProvider' => $dataProvider,
        ]);
    }

    /**
     * Displays a single Release model.
     * @param integer $id
     * @return mixed
     */
    public function actionView($id)
    {
        $model = $this->findModel($id);
        $model->create_time = date('Y-m-d H:i:s', $model->create_time);
        $model->lm_time = date('Y-m-d H:i:s', $model->lm_time);
        $model->co_time = date('Y-m-d H:i:s', $model->co_time);
        $model->end_time = date('Y-m-d H:i:s', $model->end_time);
        return $this->render('view', [
            'model' => $this->findModel($id),
        ]);
    }

    /**
     * Creates a new Release model.
     * If creation is successful, the browser will be redirected to the 'view' page.
     * @return mixed
     */
   /* public function actionCreate()
    {
        $model = new Release();

        if ($model->load(Yii::$app->request->post()) && $model->save()) {
            return $this->redirect(['view', 'id' => $model->id]);
        } else {
            return $this->render('create', [
                'model' => $model,
            ]);
        }
    }*/

    public function actionCreate()
    {
        $model = new Release();
        if ($model->load(Yii::$app->request->post())) {
            $model->create_time = strtotime(Yii::$app->request->post()['Release']['create_time']);
            $model->lm_time = strtotime(Yii::$app->request->post()['Release']['lm_time']);
            $model->co_time = strtotime(Yii::$app->request->post()['Release']['co_time']);
            $model->end_time = strtotime(Yii::$app->request->post()['Release']['end_time']);
            if ($model->save()) {
                return $this->redirect(['view', 'id' => $model->id]);
            } else {
                return $this->render('create', [
                    'model' => $model,
                ]);
            }
        } else {
                return $this->render('create', [
                    'model' => $model,
                ]);
        }
    }

    /**
     * Updates an existing Release model.
     * If update is successful, the browser will be redirected to the 'view' page.
     * @param integer $id
     * @return mixed
     */
    /*public function actionUpdate($id)
    {
        $model = $this->findModel($id);

        if ($model->load(Yii::$app->request->post()) && $model->save()) {
            return $this->redirect(['view', 'id' => $model->id]);
        } else {
            return $this->render('update', [
                'model' => $model,
            ]);
        }
    }*/
    public function actionUpdate($id)
    {
        $model = $this->findModel($id);
        if ($model->load(Yii::$app->request->post())) {

            $model->create_time = strtotime(Yii::$app->request->post()['Release']['create_time']);
            $model->lm_time = strtotime(Yii::$app->request->post()['Release']['lm_time']);
            $model->co_time = strtotime(Yii::$app->request->post()['Release']['co_time']);
            $model->end_time = strtotime(Yii::$app->request->post()['Release']['end_time']);
            if ($model->save()) {
                return $this->redirect(['view', 'id' => $model->id]);
            } else {
                return $this->render('update', [
                    'model' => $model,
                ]);
            }
        } else {
                return $this->render('update', [
                    'model' => $model,
                ]);
        }
    }

    /**
     * Deletes an existing Release model.
     * If deletion is successful, the browser will be redirected to the 'index' page.
     * @param integer $id
     * @return mixed
     */
    public function actionDelete($id)
    {
        $this->findModel($id)->delete();
        return $this->redirect(['index']);
    }

    /**
     * Finds the Release model based on its primary key value.
     * If the model is not found, a 404 HTTP exception will be thrown.
     * @param integer $id
     * @return Release the loaded model
     * @throws NotFoundHttpException if the model cannot be found
     */
    protected function findModel($id)
    {
        if (($model = Release::findOne($id)) !== null) {
            return $model;
        } else {
            throw new NotFoundHttpException('The requested page does not exist.');
        }
    }
        /**
     * 删除按钮
     * If update is successful, the browser will be redirected to the 'index' page.
     * @return mixed
     */
    public function actionAlldelete()
    {
        $artname="";
        if(isset(zhongkefu::$app->request->post()["checkboxid"])){
            $ids = zhongkefu::$app->request->post()["checkboxid"];
            $ids = explode(',',$ids);
            foreach($ids as $val){
                $models = Release::findOne(['id' => $val]);
                $artname .= $models->re_ctitle.",";
            }
            $artname = substr($artname,0,-1);
            foreach($ids as $id){
                $model = Release::findOne(['id' => $id]);
                $this->findModel($id)->delete();
            }
            Adminlog::addlog(yii::$app->user->id, '批量删除活动 '.$artname);
            return true;
        }
    }
        /**
     * 搜索按钮
     * If update is successful, the browser will be redirected to the 'index' page.
     * @return mixed
     */
    public function actionSousuo()
    {
        if (Yii::$app->request->post()) {
            $data['ReleaseSearch']['re_number'] = yii::$app->request->post()['re_number'];
            $data['ReleaseSearch']['re_ctitle'] = yii::$app->request->post()['re_ctitle'];
            $data['ReleaseSearch']['re_status'] = yii::$app->request->post()['re_status'];
            $data['ReleaseSearch']['reg_id'] = Regstand::find()->where(['reg_name' => yii::$app->request->post()['reg_id']])->asArray()->one()['reg_id'];
            $data['ReleaseSearch']['rez_id'] = Rezstand::find()->where(['rez_name' => yii::$app->request->post()['rez_id']])->asArray()->one()['rez_id'];
            $data['ReleaseSearch']['rem_id'] = Reguomin::find()->where(['rem_name' => yii::$app->request->post()['rem_id']])->asArray()->one()['rem_id'];
            $searchModel = new ReleaseSearch();
            $dataProvider = $searchModel->search($data);
            return $this->render('index', [
                'searchModel' => $searchModel,
                'dataProvider' => $dataProvider,
            ]);
        } 
    }
}
